﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace OA.Models
{
    /// <summary>
    ///  网盘, 文件&文件夹
    /// </summary>
    public class File
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        ///  文件或文件夹名
        /// </summary>
        [Required]
        [DisplayName("文件名")]
        public string Name { get; set; }
        /// <summary>
        ///  文件名缩写
        /// </summary>
        [NotMapped]
        public string AbbreviationName
        {
            get
            {
                if (Name.Length > 6)
                {
                    return Name.Substring(0, 6) + "...";
                }

                return Name;
            }
        }

        /// <summary>
        ///  后缀名, 带连接符[文件夹缺省]
        /// </summary>
        [DisplayName("后缀名")]
        public string Extension { get; set; }
        /// <summary>
        ///  路径[文件夹缺省]
        /// </summary>
        [DisplayName("文件路径")]
        public string Path { get; set; }
        /// <summary>
        ///  是否是文件夹
        /// </summary>
        [DisplayName("是否是文件夹")]
        public bool IsFolder { get; set; }
        /// <summary>
        ///  创建者姓名
        /// </summary>
        [DisplayName("创建者")]
        public string Creator { get; set; }
        /// <summary>
        ///  创建时间
        /// </summary>
        [DisplayName("创建时间")]
        public DateTime CreatDate { get; set; }

        /// <summary>
        ///  图标
        /// </summary>
        [NotMapped]
        public string Icon
        {
            get
            {
                var path = @"/Content/Resource/icons/netdisk/";

                if (IsFolder)
                {
                    return path + "file_icon.png";
                }
                else
                {
                    var ext = "";

                    switch (Extension)
                    {
                        case ".jpg":
                        case ".png":
                        case ".gif":
                        case ".bmp":
                            ext = "bmp";
                            break;
                        case ".ppt":
                        case ".pptx":
                            ext = "ppt";
                            break;
                        case ".doc":
                        case ".docx":
                            ext = "doc";
                            break;
                        case ".xls":
                        case ".xlsx":
                            ext = "xls";
                            break;
                        case ".txt":
                            ext = "txt";
                            break;
                        case ".zip":
                            ext = "zip";
                            break;
                        case ".rar":
                            ext = "rar";
                            break;
                        case ".pdf":
                            ext = "pdf";
                            break;
                        default:
                            ext = "else";
                            break;
                    }

                    return path + ext + "_icon.png";
                }
            }
        }

        /// <summary>
        ///  父文件夹Id
        /// </summary>
        [DisplayName("父文件夹")]
        public int? ParentId { get; set; }
        /// <summary>
        ///  父文件夹
        /// </summary>
        [DisplayName("父文件夹")]
        [ForeignKey("ParentId")]
        public virtual File Parent { get; set; }
        /// <summary>
        ///  子文件
        /// </summary>
        [DisplayName("子文件")]
        [InverseProperty("Parent")]
        public virtual ICollection<File> Children { get; set; }
    }
}
